package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.CommunityDao;
import cn.hs.ph.entity.Community;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class CommunityDaoimpl implements CommunityDao {
    @Override
    public int insert(Community community) {
        String sql="insert into community values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object obj[]={
                community.getCommunityName(),
                community.getCommunityProvince(),
                community.getCommunityCity(),
                community.getCommunityDistrict(),
                community.getCommunityAddress(),
                community.getCommunityLatitude(),
                community.getCommunityLongitude(),
                new Date(),
                community.getCommunityShopCount(),
                community.getCommunityAreaCode(),
                community.getCommunityCoordinates(),
                community.getCommunityStatus(),
                community.getCommunityUserCount(),
                community.getDistance()


        };



        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public int delete(int communityId) {
        String sql="delete from community where communityId=?";
        Object obj[]={communityId};
         return JDBCDruidUtil.update(sql,obj);

    }

    @Override
    public int update(Community community) {
        String sql=" update community set communityName=?,communityProvince=?,communityCity=?,communityDistrict=?,communityAddress=?,communityLatitude=?,communityLongitude=? where communityId=? ";
        Object obj[]={
                community.getCommunityName(),
                community.getCommunityProvince(),
                community.getCommunityCity(),
                community.getCommunityDistrict(),
                community.getCommunityAddress(),
                community.getCommunityLatitude(),
                community.getCommunityLongitude(),
                community.getCommunityId()
        };

        return JDBCDruidUtil.update(sql,obj);
    }

    @Override
    public List<Community> selectAll(Map<String, String> map) throws SQLException {
        List<Community> list = new ArrayList<Community>();

        String sql = "select * from community where 1=1 ";
        List params=new ArrayList<>();
        String communityName = map.get("communityName");
        if (communityName!=null&&!communityName.equals("")) {
            sql+=" and communityName like ? ";
            params.add("%"+communityName+"%");
        }

        String startTime = map.get("startTime");
        if (startTime!=null&&!startTime.equals("")) {
            sql+=" and createTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }


//分页
        String pn = map.get("pageNum");
if (pn!=null&&!pn.equals("")) {
    sql+=" limit ?,? ";
    int pageNum = Integer.parseInt(pn);
    int pageSize = Integer.parseInt(map.get("pageSize"));
    params.add((pageNum-1)*pageSize);
    params.add(pageSize);
}
        Object[] array = params.toArray();
        ResultSet query = JDBCDruidUtil.query(sql, array);
       while (true){
           if(!query.next())break;
           Community community = new Community(
                   query.getInt(1),
                   query.getString(2),
                   query.getString(3),
                   query.getString(4),
                   query.getString(5),
                   query.getString(6),
                   query.getString(7),
                   query.getString(8),
                   query.getDate(9),
                   query.getInt(10),
                   query.getInt(11),
                   query.getString(12),
                   query.getInt(13),
                   query.getInt(14),
                   query.getString(15)
           );
list.add(community);

       }
JDBCDruidUtil.close();
return list;
    }

    @Override
    public int selectCount(Map<String, String> map) throws SQLException {
int count = 0;
        String sql = "select count(1) from community where 1=1 ";
        List<Object> params=new ArrayList<>();
        String communityName = map.get("communityName");
        if (communityName!=null&&!communityName.equals("")) {
            sql+=" and communityName like ?";
            params.add("%"+communityName+"%");
        }

        String startTime = map.get("startTime");
        if (startTime!=null&&!startTime.equals("")) {
            sql+=" and createTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }


        Object[] array = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, array);
   while (true){
       if (!res.next()) break;
       count=res.getInt(1);

   }

        return count;
    }
}
